#ifndef UPLOAD_H
#define UPLOAD_H

#include <QObject>
#include <QDebug>
#include <QMessageBox>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QThread>

#include "filedata_t.h"
#include "configfile.h"

class Upload : public QObject
{
    Q_OBJECT

public:
    enum Status {
        NO_ERROR,
        /* customer error */
        INVALID_NAME,
        INVALID_PHPSESSID,
        INVALID_UPLOAD_DIR,
        FILE_IS_FOLDER,
        JSON_ERROR,

        /* network error */
        RESPONSE_ERROR,

        UNDEFINED_ERROR = -1,
    };
    static inline QString errorString(Status code) {
        switch (code) {
        case NO_ERROR               : return "Succeed";
        case INVALID_NAME           : return "Invalid user name";
        case INVALID_PHPSESSID      : return "Invalid PHPSESSID";
        case INVALID_UPLOAD_DIR     : return "Invalid upload directory";
        case FILE_IS_FOLDER         : return "Download file is folder";
        case UNDEFINED_ERROR        : return "Undefined error";
        default                     : return "The mistake of the mistake";
        }
    }

public:
    explicit Upload(QObject *parent = nullptr);
    ~Upload();
    void reset();
    void setFilenames(QStringList filenames);
    void setUploadDir(QString uploadDir);
    Status startUpload();
    bool isUploading();


signals:
    void finished(int code, QString errorString);
private slots:
    void uploadResponse(QNetworkReply *reply);

private: /* var */
    ConfigFile              *settings;
    bool                    uploading;
    QString                 uploadDir;
    QStringList             filenames;
    QNetworkAccessManager   *netManager;
    QNetworkRequest         *baseRequest;
    QString                 name;
    QString                 phpSessionID;

private: /* func */
    Status init();

};

#endif // UPLOAD_H
